// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var top = require("../top/top_main");
	var footer = require("../footer/footer_main");
	var constant = require("constant");
	var css_product = require("./productDetail.css");
	var css_common = require();
	var tools = require("tools");
	var productId;
	var financeState;
	var pageIndex = 1,
		pageSize = 8,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function() {
		top.init(1);
		footer.init();
		router.init();
		productId = base.getUrlParam("productId");
		financeState = base.getUrlParam("financeState");
		$("#productPage").addClass("nav_active");
		$("#head").removeClass("none");
		$("#content").removeClass("none");
		$("#footer").removeClass("none");

		inquireFinanceProductDetail();
		initEvent();
	};

	var initEvent = function() {

		$(".main").on("click", function() {
			location.href = "../main/main.html";
		});

	};

	//查询产品费用详情
	var inquireFinanceProductExpensePageList = function() {
		base.fnAjax({
			action: "inquireFinanceProductExpensePageList.json",
			nocode: true,
			oData: {
				productId: productId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#mainFinanceProductListTpl").html();
					var html = juicer(tpl, data);
					$("#mainBulkstockListBody").html(html);
					//					totalPageCount = data.totalPageCount;
					//					totalRecordCount = data.totalRecordCount;
					//根据右边的内容多来设置左边图片的大小
					//					var height = $(".right_message").height();
					//					$(".left_message").css("height", height);

					//					base.pageControl(
					//						$("#pagenation"),
					//						totalRecordCount,
					//						data.pageIndex,
					//						data.pageSize,
					//						function(p) {
					//							pageIndex = p;
					//							inquireFinanceProductPageList();
					//						});

					$(".instantlyFinance").unbind("click").click(function() {
						var productId = $(this).attr("id");
						location.href = '../apply/apply_main.html?productId=' + productId;

					});

				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	//查询产品详情
	var inquireFinanceProductDetail = function() {
		base.fnAjax({
			action: "inquireFinanceProductDetail.json",
			nocode: true,
			oData: {
				productId: productId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var fs = data.datas[0].fileUuids.split(',');
					var d = [];
					var f = {};
					$.each(fs, function() {
						var _this = this;
						if ($.trim(_this) != "") {
							var _this = this;
							f.fileUuid = _this;
							f.fileName = tools.inquireFile(_this);
							d.push(f);
						}
					});
					data.datas[0].d = d;
					
					if(financeState!=null && financeState >= 3 && financeState !=4){
						var fs = data.datas[0].sampleUuids.split(',');
						var d1 = [];
						var f = {};
						$.each(fs, function() {
							var _this = this;
							if ($.trim(_this) != "") {
								var _this = this;
								f.fileUuid = _this;
								f.fileName = tools.inquireFile(_this);
								d1.push(f);
							}
						});
						data.datas[0].d1 = d1;
					}
					
					var tpl = $("#mainFinanceProductListTpl").html();
					juicer.register("getValueByKey", constant.getValueByKey);
					var html = juicer(tpl, data);
					$("#mainBulkstockListBody").html(html);

					$(".instantlyFinance").unbind("click").click(function() {
//						var productId = $(this).attr("id");
//						location.href = '../apply/apply_main.html?productId=' + productId;
						var productType = data.datas[0].productType;
						if (productType == "6") {
								location.href = '../apply/apply_main1.html?productType=' + productType;//代销
							} else if (productType == "5") {
								location.href = '../apply/apply_main2.html?productType=' + productType;//代采
							} else {
								location.href = '../apply/apply_main.html?productType=' + productType;
							}
					});

					$.each($("table"), function() {
						var $this = $(this);
						if ($this.find("tr").length <= 1) {
							$this.parent().prev().remove();
							$this.parent().remove();
						}
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	return {
		init: init
	};
});